using System;
using System.Threading.Tasks;
using Grpc.Core;
using Retry;

namespace Server;

public class RetrierService : Retrier.RetrierBase
{
    private readonly Random _random = new Random();

    public override Task<Response> DeliverPackage(Package request, ServerCallContext context)
    {
        const double deliveryChance = 0.5;
        if (_random.NextDouble() > deliveryChance)
        {
            throw new RpcException(new Status(StatusCode.Unavailable, $"- {request.Name}"));
        }

        return Task.FromResult(new Response
        {
            Message = $"+ {request.Name}"
        });
    }
}
